package com.tf_cut.backend.platform.service.impl;


import com.tf_cut.backend.common.service.CommonServiceImpl;
import com.tf_cut.backend.mapper.GrantDetailMapper;
import com.tf_cut.backend.platform.pojo.GrantDetail;
import com.tf_cut.backend.platform.repository.GrantDetailRepository;
import com.tf_cut.backend.platform.service.GrantDetailService;
import com.tf_cut.backend.platform.vo.GrantDetailVo;
import com.tf_cut.backend.platform.vo.OrderDetailVO;
import com.tf_cut.backend.platform.vo.PersonalTaxVo;
import com.tf_cut.backend.service.service.ServicePersonalIncomeTaxSetingService;
import com.tf_cut.backend.service.vo.ServicePersonalIncomeTaxSetingVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
@Slf4j
public class GrantDetailServiceImpl extends CommonServiceImpl<GrantDetailVo, GrantDetail, String> implements GrantDetailService {

    @PersistenceContext
    private EntityManager em;
    @Autowired
    private GrantDetailRepository grantDetailRepository;
    @Autowired
    private GrantDetailMapper grantDetailMapper;
    @Autowired
    private ServicePersonalIncomeTaxSetingService servicePersonalIncomeTaxSetingService;
    @Override
    public List<GrantDetailVo> queryGrantDetailList(GrantDetailVo svo) {
        List<GrantDetailVo> list = grantDetailMapper.queryGrantDetailList(svo);
        for(int i=0;i<list.size();i++){
        	//先去掉半角空格，再去掉全角空格账号
        	list.get(i).setPayeeAccount(list.get(i).getPayeeAccount().replace(" ", "").replace("　", ""));
        	//先去掉半角空格，再去掉全角空格姓名
        	list.get(i).setPayeeName(list.get(i).getPayeeName().replace(" ", "").replace("　", ""));
        	//先去掉半角空格，再去掉全角空格开户行
        	list.get(i).setPayeeAccountBankName(list.get(i).getPayeeAccountBankName().replace(" ", "").replace("　", ""));
        	//先去掉半角空格，再去掉全角空格手机号
        	list.get(i).setPayeeMobile(list.get(i).getPayeeMobile().replace(" ", "").replace("　", ""));
        	//先去掉半角空格，再去掉全角空格身份证
        	list.get(i).setCardCode(list.get(i).getCardCode().replace(" ", "").replace("　", ""));
        }
        return list;
    }

    @Override
    public void updateGrantDetailBatch(List<GrantDetailVo> list) {
        grantDetailMapper.updateGrantDetailBatch(list);
    }
    
    @Override
    public List<PersonalTaxVo> queryByName(String payeeName, String serviceName,String startDate, String endDate, String cardCode, String startaxAmount, String endtaxAmount, String startaxMoney, String endtaxMoney) {
        List<PersonalTaxVo> list= grantDetailMapper.queryByName(payeeName,serviceName,startDate,endDate,cardCode,startaxAmount,endtaxAmount,startaxMoney,endtaxMoney);

        for (PersonalTaxVo v:list ) {
            ServicePersonalIncomeTaxSetingVo vo= new ServicePersonalIncomeTaxSetingVo();
            vo.setServiceId(v.getServiceId());
            List<ServicePersonalIncomeTaxSetingVo> taxList=servicePersonalIncomeTaxSetingService.queryByServiceId(vo);
            if(taxList!=null && taxList.size()>0){
                for (ServicePersonalIncomeTaxSetingVo tvo:taxList) {
                    Double min=0.0;
                    if(tvo.getIsMin()==1){
                        min=tvo.getMin()-1;
                    }else{
                        min=tvo.getMin();
                    }
                    Double max=0.0;
                    if(tvo.getIsMin()==1){
                        max=tvo.getMax()+1;
                    }else{
                        max=tvo.getMax();
                    }
                    //不含税金额
                    double money=v.getPayAmount()/1.03;
                    if(min<money && max>money){
                        if(tvo.getSpare1()!=null && !"".equals(tvo.getSpare1())){
                            v.setTax(Double.parseDouble(tvo.getSpare1()));
                        }else{
                            v.setTax(0.0);
                        }
                        break;
                    }
                }
            }else{
                v.setTax(0.0);
            }
        }
        return list;
    }

    @Override
    public List<OrderDetailVO> queryByNames(String startDate, String endDate,String tax) {
    	
    	double sum=Integer.parseInt(tax);
    	sum=1+sum/100;
    	
        List<OrderDetailVO> list= grantDetailMapper.queryByNames(startDate,endDate);


        for (OrderDetailVO v:list ) {
            ServicePersonalIncomeTaxSetingVo vo= new ServicePersonalIncomeTaxSetingVo();
            vo.setServiceId(v.getServiceId());
            List<ServicePersonalIncomeTaxSetingVo> taxList=servicePersonalIncomeTaxSetingService.queryByServiceId(vo);
            if(taxList!=null && taxList.size()>0){
                for (ServicePersonalIncomeTaxSetingVo tvo:taxList) {
                    Double min=0.0;
                    if(tvo.getIsMin()==1){
                        min=tvo.getMin()-1;
                    }else{
                        min=tvo.getMin();
                    }
                    Double max=0.0;
                    if(tvo.getIsMin()==1){
                        max=tvo.getMax()+1;
                    }else{
                        max=tvo.getMax();
                    }
                    //不含税金额
                    double money=v.getPayAmount1()/sum;
                    if(min<money && max>money){
                        if(tvo.getSpare1()!=null && !"".equals(tvo.getSpare1())){
                            v.setTax(Double.parseDouble(tvo.getSpare1()));
                        }else{
                            v.setTax(0.0);
                        }
                        break;
                    }else{
                    	v.setTax(0.0);
                    }
                }
            }else{
                v.setTax(0.0);
            }
        }
        return list;
    }

	@Override
	public OrderDetailVO findById(String id) {
		return grantDetailMapper.findById(id);
	}

	@Override
	public List<OrderDetailVO> findByTime() {
		return grantDetailMapper.findByTime();
	}

	@Override
	public List<PersonalTaxVo> findByQueryTime() {
		return grantDetailMapper.findByQueryTime();
	}
}
